package com.fatdetection.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.annotation.AuthAdmin;
import com.fatdetection.demo.annotation.AuthOperator;
import com.fatdetection.demo.annotation.SuperAdmin;
import com.fatdetection.demo.bean.Admin;
import com.fatdetection.demo.bean.LayuiTableRst;
import com.fatdetection.demo.bean.RstJson;
import com.fatdetection.demo.service.AdminService;
import com.fatdetection.demo.util.RstMsgUtils;
import com.fatdetection.demo.util.TokenUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@AuthAdmin
@AuthOperator
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    TokenUtils tokenUtils;

    @GetMapping("/selectPage")
    public Object selectPage(Integer current, Integer size){
        Page<Admin> adminPage = adminService.selectPage(current, size);
        LayuiTableRst layuiTableRst = new LayuiTableRst();
        layuiTableRst.setCode(0);
        layuiTableRst.setCount((int) adminPage.getTotal());
        layuiTableRst.setMsg("获取成功");
        layuiTableRst.setData(adminPage.getRecords());
        return layuiTableRst;
    }
    @ApiOperation(value = "密码验证并修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPwd", value = "旧密码", required = true),
            @ApiImplicitParam(name = "newPwd", value = "新密码", required = true),
            @ApiImplicitParam(name = "username", value = "用户名", required = true)
    })
    @PostMapping("/updatePwd")
    public Object upadtePwd(String oldPwd, String newPwd, String username){
        boolean b = adminService.updatePassword(oldPwd, newPwd, username);
        if(b){
            return RstMsgUtils.success("密码修改成功", "");
        }else{
            return RstMsgUtils.error("密码修改失败");
        }
    }

    @ApiOperation(value = "管理员登录获取token", notes = "该接口负责管理员登录获取token，以及进行登录检验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
        })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(String username, String password) {
        RstJson rstJson = new RstJson();
        Admin result = adminService.login(username, password);
        if (result != null) {
            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("username", result.getUsername());
            tokenMap.put("role", String.valueOf(result.getRole()));
            String token = tokenUtils.geneToken(tokenMap);
            Map<String, Object> rstMap = new HashMap<>();
            rstMap.put("userInfo", result);
            rstMap.put("token", token);
            rstJson = RstMsgUtils.success("登录成功", rstMap);
            // 生成 token 凭证
        } else {
            rstJson = RstMsgUtils.error("登录失败");
        }
        return rstJson;
    }

    @ApiOperation(value = "管理员添加", notes = "管理员添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = true)
    })
    @PostMapping("/add")
    @SuperAdmin
    public Object registerAdmin(String username, String password, String nickname, String company, String phone) {
        Admin admin = new Admin(username, password, nickname, company, phone, null, null, null);
        int count = adminService.registerAdmin(admin);
        if (count > 0) {
            return RstMsgUtils.success("注册成功", "");
        } else if (count == -2) {
           return RstMsgUtils.error("该用户已被注册");
        } else {
            return RstMsgUtils.error("注册失败");
        }
    }
    @ApiOperation(value = "管理员删除", notes = "管理员删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true)
    })
    @PostMapping("/del")
    @SuperAdmin
    public Object delAdmin(String username) {
        int count = adminService.delAdmin(username);
        if (count > 0) {
            return RstMsgUtils.success("删除成功", "");
        } else {
            return RstMsgUtils.error("删除失败");
        }
    }
    @ApiOperation(value = "管理员更新", notes = "管理员信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = false),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = false)
    })
    @PostMapping("/update")
    public Object updateAdmin(Admin admin) {
        int count = adminService.updateAdmin(admin);
        if (count > 0) {
            return RstMsgUtils.success("修改成功", "");
        } else {
            return RstMsgUtils.error("修改失败");
        }
    }

    @GetMapping("/findAll")
    @SuperAdmin
    public List<Admin> getAllAdmin() {
        return adminService.findAll();
    }

    @GetMapping("/findByUsername/{username}")
    @SuperAdmin
    public Admin getByUsername(@PathVariable("username") String username) {
        return adminService.findAdminByusername(username);
    }

    @GetMapping("/findByNickname/{nickname}")
    @SuperAdmin
    public List<Admin> getByNickname(@PathVariable("nickname") String nickname) {
        return adminService.findAdminByNickname(nickname);
    }

}
